//
//  AddContactsViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-1.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "AddContactsViewController.h"
#import "ContactsDateBase.h"

@interface AddContactsViewController ()
{
    //从数据库中读出来的暂存的字符串
    NSString *tempUserName;
    NSString *tempDisplayName;
    NSString *tempMarkName;
    NSString *tempIpPort;
    NSString *tempOnlinestatus;
    NSString *tempIsFriend;
}

@end

@implementation AddContactsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"add contacts", @"添加联系人 Add contacts");
    }
    return self;
}

//通过号码初始化 将传过来的号码放入到号码栏内
- (id)initWithNumber:(NSString *)number
{
    self = [super init];
    if (self)
    {
        self.tempNumber = [[NSString alloc] init];
        self.tempNumber = number;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.tempNumber.length > 0)
    {
        self.nameTextField.text = self.tempNumber;
    }
    
    ContactsDateBase *contactsDB = [ContactsDateBase shareContactsDataBase];
    //用数据库赋值
    [contactsDB readDB];
}

- (IBAction)sameIpPortButtonPressed:(id)sender
{
    if (!self.isSameIpPort)
    {
        [self.isSameIPportButton setBackgroundImage:[UIImage imageNamed:@"选中.png"] forState:UIControlStateNormal];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *tempStrIP = [[NSString alloc]
                               init];
        tempStrIP = [defaults valueForKey:@"SAVEIPADDRESS"];
        self.ipPortTextField.text = tempStrIP;
    }
    else if(self.isSameIpPort)
    {
        [self.isSameIPportButton setBackgroundImage:[UIImage imageNamed:@"选择.png"] forState:UIControlStateNormal];
        self.ipPortTextField.text = @"";
    }
    self.isSameIpPort = !self.isSameIpPort;
}

//添加联系人操作
- (IBAction)addButtonPressed:(id)sender
{
    //先判断是否是登陆状态，如果不是登陆状态则提示请登陆
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    BOOL registerStatus = [defaults boolForKey:@"REGISTERSTATUS"];
//    if (registerStatus)
//    {
        ContactsDateBase *contactsDB = [ContactsDateBase shareContactsDataBase];
        if (self.nameTextField.text.length > 0 && self.ipPortTextField.text.length > 0)
        {
            for (int i = 0 ; i < contactsDB.userNameArray.count ; i++)
            {
                if ([self.nameTextField.text isEqualToString:contactsDB.userNameArray[i]] && [self.ipPortTextField.text isEqualToString:contactsDB.ipPortArray[i]])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"has been added through the user in the domain, do not repeat the add", @"已经添加过该域名下的该用户，请勿重复添加 Has been added through the user in the domain, do not repeat the add") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
                    [alert show];
                    return;
                }
                else if([self.numberTextField.text isEqualToString:contactsDB.markNameArray[i]])
                {
                    //数据库进行更新
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"remarks the same name already exists, in order to avoid data corruption. Please modify Remarks name to add, thank you", @"已存在相同的备注名，为了避免数据出错。请修改备注名再添加，谢谢合作 Remarks the same name already exists, in order to avoid data corruption. Please modify Remarks name to add, thank you") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
                    [alert show];
                    return;
                }
            }
            
//            [[SIPInterface shareSIPInterface] addFriendsWithUserName:self.nameTextField.text andDisPlayName:@"" andIp:self.ipPortTextField.text];
            if (self.numberTextField.text.length > 0)
            {
                //放到联系人数据库内
                [contactsDB saveFriendIntoDBWithUserName:self.nameTextField.text andMarkName:self.numberTextField.text andIp:self.ipPortTextField.text];
            }
            else
            {
                //放到联系人数据库内
                [contactsDB saveFriendIntoDBWithUserName:self.nameTextField.text andMarkName:@"" andIp:self.ipPortTextField.text];
            }
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"好友请求已经发送" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"add contacts success", @"添加联系人成功 Add contacts success") delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            
            double delayInSeconds = 1.0;
//            __block UIViewController *bself = self;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self.navigationController popViewControllerAnimated:YES];
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
            
            
            
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"please fill out the complete contents", @"请将内容填写完整 Please fill out the complete contents") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
            [alert show];
        }

//    }
//    else
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"请先登陆" delegate:self cancelButtonTitle:@"明白了" otherButtonTitles: nil];
//        [alert show];
//    }
}

//取消按钮将返回
- (IBAction)cancelButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark textFieldResponder
//当姓名输入完成时 将键盘缩回
- (IBAction)nameTextfieldDidEnd:(id)sender
{
    [self.nameTextField resignFirstResponder];
}
//当号码输入完成时 将键盘缩回
- (IBAction)numberTextFieldDidEnd:(id)sender
{
    [self.numberTextField resignFirstResponder];
}
- (IBAction)ipTextFieldDidEnd:(id)sender
{
    [self.ipPortTextField resignFirstResponder];
}
//当点击背景时 将两个输入栏的键盘缩回
- (IBAction)backgroundTouchDown:(id)sender
{
    [self.nameTextField resignFirstResponder];
    [self.numberTextField resignFirstResponder];
    [self.ipPortTextField resignFirstResponder];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
